package com.trkj.service.cf;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.cf.CoursearrangementDAO;
import com.trkj.dao.cf.TraininggactivityDAO;
import com.trkj.dao.wdm.StaffInformationDAO;
import com.trkj.pojo.Coursearrangement;
import com.trkj.pojo.LaborTurnover;
import com.trkj.pojo.StaffInformation;
import com.trkj.pojo.Traininggactivity;
import com.trkj.service.ServiceException;
import com.trkj.vo.cf.CoursearrangementVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class CoursearrangementService {
	@Autowired
	private   CoursearrangementDAO coursearrangementDAO;

	@Autowired
	private StaffInformationDAO staffInformationDAO;
	@Autowired
	private TraininggactivityDAO traininggactivityDAO;
	public long pageCount() {
		 
		return coursearrangementDAO.count();
	}
	//分页
		public List<CoursearrangementVo> CoursearrangementPage(int page, int rows) throws ServiceException {
			try {
			 
				List<Object[]> list = coursearrangementDAO.coursearrangementQuery(new PageRequest(page - 1, rows));
					 List<CoursearrangementVo> listvo =new ArrayList<>();
					 for (Object[] pages : list) {
						 Coursearrangement  cou=  (Coursearrangement) pages[0];
						 Traininggactivity  train=(Traininggactivity) pages[2];
						 StaffInformation   staff= (StaffInformation) pages[1];
						 CoursearrangementVo vo =new CoursearrangementVo(); 
						 vo.setCourseaId(cou.getCourseaId());
						 vo.setCourseaMakeup(cou.getCourseaMakeup());
						 vo.setCourseaTestscores(cou.getCourseaTestscores());
						 vo.setCourseaQualifiedscore(cou.getCourseaQualifiedscore());
						 vo.setCourseaState(cou.getCourseaState());
						 vo.setStainId(staff.getStainId());
						 vo.setStainName(staff.getStainName());
						 vo.setTrainingId(train.getTrainingId());
						 vo.setTrainingName(train.getTrainingName());
						 listvo.add(vo);
						 
					 }
			
				return listvo;
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
		}
		//新增
		public void Coursearrangementadd(CoursearrangementVo vo, Long stainId,String stainName,Long trainingId, String trainingName, Integer courseaQualifiedscore,
				Integer courseaTestscores, Integer courseaMakeup, Integer courseaState)throws ServiceException {
			try {
				System.out.println("llls"+stainName+trainingName+courseaQualifiedscore+courseaTestscores+courseaMakeup+courseaState);
					StaffInformation staff= staffInformationDAO.findOne(vo.getStainId());
					Traininggactivity tra= traininggactivityDAO.findOne(vo.getTrainingId());
					Coursearrangement  cou=new Coursearrangement();// coursearrangementDAO.findOne(vo.getCourseaId());
					System.out.println( staff +"  " +tra );
						cou.setStaffInformation(staff);
						cou.setTraininggactivity(tra);
						cou.setCourseaQualifiedscore(new BigDecimal(courseaQualifiedscore)    );
						cou.setCourseaTestscores(new BigDecimal(courseaTestscores));
						cou.setCourseaMakeup(new BigDecimal(courseaMakeup));
						cou.setCourseaState(new BigDecimal(courseaState));
						coursearrangementDAO.save(cou);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
			
		}
		//修改
		public void Coursearrangementupdate(CoursearrangementVo vo,Long courseaId, Long stainId, String stainName, Long trainingId,
				String trainingName, Integer courseaQualifiedscore, Integer courseaTestscores, Integer courseaMakeup,
				Integer courseaState) {
			StaffInformation staffs=  staffInformationDAO.findOne(vo.getStainId());
			Traininggactivity tras=  traininggactivityDAO.findOne(vo.getTrainingId());
			Coursearrangement  cous= coursearrangementDAO.findOne(vo.getCourseaId());
					cous.setStaffInformation(staffs);
					cous.setTraininggactivity(tras);
					cous.setCourseaQualifiedscore(new BigDecimal(courseaQualifiedscore)  );
					cous.setCourseaTestscores(new BigDecimal(courseaTestscores));
					cous.setCourseaMakeup(new BigDecimal(courseaMakeup));
					cous.setCourseaState(new BigDecimal(courseaState));
					coursearrangementDAO.save(cous);
		}
		// 搜索
		public List<Object[]> filedByName1(String stainName) {
			
			return coursearrangementDAO.queryByName("%"+stainName+"%");
		}
		// 搜索
		public List<Object[]> filedByName(String stainName) {
					
			return coursearrangementDAO.queryByName("%"+stainName+"%");
			}
		//删除
		public void Coursearrangementremove(Long courseaId) {
			coursearrangementDAO.delete(courseaId);
		}
		//查询
		public List<StaffInformation> Coursearrangement() {
			List<Object[]> map= coursearrangementDAO.findAll2();
			List<StaffInformation> list= new ArrayList<>();
				for (Object[] objects : map) {
					StaffInformation s=	(StaffInformation) objects[0];
					LaborTurnover l=(LaborTurnover) objects[1];
					StaffInformation sta=new StaffInformation();
					sta.setStainId(s.getStainId());
					sta.setStainName(s.getStainName());
					list.add(sta);
				}
			return list;
		}
		
}
